package btd.game.dlgs;

import btd.game.activities.PBoardAct;
import btd.game.activities.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;

public class ReStartDlg extends Dialog {
	private PBoardAct owner = null;
	private boolean isTimeout = false;
	public boolean canClose = false;
	public ReStartDlg(Context context) {
		super(context);
		owner = (PBoardAct) context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		String title = owner.getString(R.string.restart);
		StringBuffer sb = new StringBuffer();
		sb.append("      ");
		sb.append(title);
		sb.append("      ");
		this.setTitle(sb.toString());
		new Thread(closeRunnable).start();
	}
	
	private Runnable closeRunnable = new Runnable() {
		@Override
		public void run() {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			isTimeout = true;
			close();
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return true;
	}

	public void close() {
		if(isTimeout && canClose) {
			if(owner.view.status) {
				owner.lStartSubTime();
			} else {
				owner.dStartSubTime();
			}
			this.cancel();
		}
	}
}
